//------------------------------------
function spinEvent(objSpinButton, spinevent)
{
//	alert(objSpinButton.id + '\n' + spinevent);

//	var element = objSpinButton.id.replace('-spinButtons','');
	var element = spinevent.target.id.replace('-spinButtons','');
	var spin =    spinevent.target.getAttribute('class');

	var objControl = document.getElementById('ifl_expires_' + element);
	
	if (spin=='up') 
			objControl.value++;

	if (spin=='down') 
			objControl.value--;

	var fn = 'Change' + element;
	if (typeof(fn) == 'function')
		window[fn](objControl);
	
	switch (element.toLowerCase())
  {
  	case 'day': case 'year': 
			SetDateField();
			break;
	  case 'hours': case 'minutes': case 'seconds':
			SetTimeField();
			break;
			
		default:
			alert("Invalid date type");
	}
	
//	ChangeSeconds(this);SetTimeField()
	
	

	
	
}

//------------------------------------

//------------------------------------------------------------------------------
function myPrepareDatePickerFunction( )
{
	var datePickerPopup = document.getElementById( "oe-date-picker-popup" );
	datePickerPopup.setAttribute( "value", (new Date(document.getElementById("ifl_expires_date").value)) );
}
//------------------------------------------------------------------------------

//-------------------------------------------------------------------------------------
function yourCommandFunction( datepopup )
{
	var newDate =  datepopup.value;
//	var tempSrc = document.getElementById("start-date-text");
	var tempSrc = document.getElementById("ifl_expires_date");
	var getMonth = newDate.getMonth();
	
	tempSrc.value= GetDay(newDate.getDay()) + ', ' + GetMonth(getMonth) + ' ' + newDate.getDate() + ", " +  newDate.getFullYear();
	FixDate();
	// datepopup.value is a Date object with 
	// the year, month, day set to the user selection
}
//-------------------------------------------------------------------------------------


//---------------------------------------------------------------------
function ChangeSeconds(objText)
{

	ValidateSeconds(objText);

	if (objText.value.length<1)
	{
		objText.value = document.getElementById('ifl_expires_time').value.substring(6,8);
		alert("Allowed values are: 00 thru 59");	
	}


	if (objText.value < 0 || objText.value > 59) 
	{
		alert('bobo');
		objText.value = document.getElementById('ifl_expires_time').value.substring(6,8);
		alert('Allowed values are: 00 thru 59');	
	}

	objText.value = right('00' + objText.value,2)

}
//---------------------------------------------------------------------

//---------------------------------------------------------------------
function ValidateSeconds(objText)
{
	var re = new RegExp("-[^0-9]", "g");
	if (!IsNumeric(objText.value))
		objText.value = objText.value.replace(re, "");
	
	if (objText.value.length>2)
		objText.value = left(objText.value,2)

	if (objText.value>59)
		objText.value = '59';

	if (objText.value<0)
		objText.value = '00';

}
//---------------------------------------------------------------------



//---------------------------------------------------------------------
function ChangeMinutes(objText)
{
	ValidateMinutes(objText);

	if (objText.value.length<1)
	{
		objText.value = document.getElementById('ifl_expires_time').value.substring(3,5);
		alert("Allowed values are: 00 thru 59");	
	}


	if (objText.value < 0 || objText.value > 59) 
	{
		objText.value = document.getElementById('ifl_expires_time').value.substring(3,5);
		alert('Allowed values are: 00 thru 59');	
	}

	objText.value = right('00' + objText.value,2)

}
//---------------------------------------------------------------------

//---------------------------------------------------------------------
function ValidateMinutes(objText)
{
	var re = new RegExp("-[^0-9]", "g");
	if (!IsNumeric(objText.value))
		objText.value = objText.value.replace(re, "");
	
	if (objText.value.length>2)
		objText.value = left(objText.value,2)

	if (objText.value>59)
		objText.value = '59';

	if (objText.value<0)
		objText.value = '00';


}
//---------------------------------------------------------------------



//---------------------------------------------------------------------
function ChangeHours(objText)
{
	ValidateHours(objText);

	if (objText.value.length<1)
	{
		objText.value = document.getElementById('ifl_expires_time').value.substring(0,2);
		alert("Allowed values are: 00 thru 23");	
	}


	if (objText.value < 0 || objText.value > 23) 
	{
		objText.value = document.getElementById('ifl_expires_time').value.substring(0,2);
		alert('Allowed values are: 00 thru 23');	
	}

	objText.value = right('00' + objText.value,2)

}
//---------------------------------------------------------------------

//---------------------------------------------------------------------
function ValidateHours(objText)
{
	var re = new RegExp("-[^0-9]", "g");
	if (!IsNumeric(objText.value))
		objText.value = objText.value.replace(re, "");
	
	if (objText.value.length>2)
		objText.value = left(objText.value,2)

	if (objText.value>23)
		objText.value = '23';

	if (objText.value<0)
		objText.value = '00';


}
//---------------------------------------------------------------------



//--------------------------------------------------------------
function ChangeDay(objText)
{
	
	ValidateDay(objText);
	
	if  (objText.value.length == 1)
			objText.value = right('00' + objText.value,2)
	
	if (objText.value.length < 1 || objText.value < 1) 
	{
		objText.value = (new Date(document.getElementById('ifl_expires_date').value)).getDate();
//		alert("Please enter a valid day");	
	}

	objText.value = right('00' + objText.value,2)

}
//--------------------------------------------------------------



//---------------------------------------------------------------------
function ValidateDay(objText)
{
	
	var re = new RegExp("-[^0-9]", "g");
	if (!IsNumeric(objText.value))
		objText.value = objText.value.replace(re, "");

	var d = document;
	var year = d.getElementById('ifl_expires_Year').value;
	var month = MonthToNumber(d.getElementById('ifl_expires_Month').value);
		
  var days = new Array(31, ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0 ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

  if (objText.value > days[month])
  	objText.value = days[month];

  if (IsNumeric(objText.value) && objText.value < 0)
  			objText.value = "01";


}
//---------------------------------------------------------------------



//---------------------------------------------------------------------
function ChangeYear(objText)
{
	
	ValidateYear(objText);
	if (objText.value.length < 4 || objText.value.length > 4) 
	{
		objText.value = (new Date(document.getElementById('ifl_expires_date').value)).getFullYear();
		alert("Please enter a sensible valid year");	
	}
}
//---------------------------------------------------------------------

//---------------------------------------------------------------------
function ValidateYear(objText)
{
	var re = new RegExp("[^0-9]", "g");
	if (!IsNumeric(objText.value))
		objText.value = objText.value.replace(re, "");
}
//---------------------------------------------------------------------


//---------------------------------------------------------------
function SetTimeField()
{
	var d = document;
	d.getElementById('ifl_expires_time').value =  d.getElementById('ifl_expires_Hours').value + ':' + 
																								d.getElementById('ifl_expires_Minutes').value + ':' + 
																								d.getElementById('ifl_expires_Seconds').value;
}



//---------------------------------------------------------------
function SetDateField()
{
	var d = document;
	
	d.getElementById('ifl_expires_date').value =  d.getElementById('ifl_expires_Month').value + ' ' + 
																								d.getElementById('ifl_expires_Day').value + ', ' + 
																								d.getElementById('ifl_expires_Year').value;
	
	d.getElementById('ifl_expires_DayOfWeek').value = GetDay((new Date(d.getElementById('ifl_expires_date').value)).getDay());

	d.getElementById('ifl_expires_date').value =  d.getElementById('ifl_expires_DayOfWeek').value + ', ' + 
																								d.getElementById('ifl_expires_Month').value + ' ' + 
																								d.getElementById('ifl_expires_Day').value + ', ' + 
																								d.getElementById('ifl_expires_Year').value;
}

//---------------------------------------------------------------
function FixDate() 
{
	var d = document;

	var expr_date = (new Date(d.getElementById('ifl_expires_date').value));
		
	d.getElementById('ifl_expires_DayOfWeek').value 	= GetDay(expr_date.getDay());
	d.getElementById('ifl_expires_Month').value 			= GetMonth(expr_date.getMonth());
	d.getElementById('ifl_expires_Day').value 				= expr_date.getDate();
	d.getElementById('ifl_expires_Year').value 				= expr_date.getFullYear();		
}

//------------------------------------------------------------------
function FixTime()
{
	var d = document;

//	var expr_time = (new Date( 'Thursday, January 01, 1970 ' +d.getElementById('ifl_expires_time').value));
	var expr_time = d.getElementById('ifl_expires_time').value;
	
	d.getElementById('ifl_expires_Hours').value 					= expr_time.substring(0,2);
	d.getElementById('ifl_expires_Minutes').value 				= expr_time.substring(3,5);
	d.getElementById('ifl_expires_Seconds').value 				= expr_time.substring(6,8);
}

//------------------------------------------------------------------ 
function GetDay(intDay){
	var DayArray = new Array("Sunday", "Monday", "Tuesday", "Wednesday", 
	                         "Thursday", "Friday", "Saturday");
	return DayArray[intDay];
}

//------------------------------------------------------------------
function GetMonth(intMonth){
	var MonthArray = new Array("January", "February", "March",
                               "April", "May", "June",
                               "July", "August", "September",
                               "October", "November", "December");
	return MonthArray[intMonth];
}


//------------------------------------------------------------------
function MonthToNumber(strMonth){
	var MonthArray = new Array("january", "february", "march",
                               "april", "may", "june",
                               "july", "august", "september",
                               "october", "november", "december");

	strMonth = strMonth.toLowerCase() ;
	for (var i=0; i<11;i++)
	{
		if (MonthArray[i] == strMonth || MonthArray[i].indexOf(strMonth) == 0)
				return i;
	}

	return -1;

}

//------------------------------------------------------------------

function getDateStr(datestr){
	var dStr = new Date(datestr);
  var year = dStr.getYear();
  if(year<1000) year+=1900;
  var retStr = GetMonth(dStr.getMonth()) + " " + dStr.getDate();
  retStr += ", " + year;
  return retStr;
}
//-----------------------------------------------------------------------------

function getTimeStr(datestr){
	var dStr = new Date(datestr);
  var retStr = right('00' + dStr.getHours(),2) + ':' + right('00' + dStr.getMinutes(),2) + ":" + right('00' + dStr.getSeconds(),2);
  return retStr;
}
//-----------------------------------------------------------------------------


//------------------------------------------------------------------

	//check for valid numeric strings	
function IsNumeric(strString)
{
   var strValidChars = "0123456789";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (var i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
}

//------------------------------------------------------------------

//---------------------------------
function right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}
//---------------------------------

function left(str, n)
{
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}
//---------------------------------

//---------------------------------------------------------
function trim(s) {
  
  s = String(s);
  
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}
//---------------------------------------------------------

//-------------------------------------------------
// http://www.flws.com.au/showusyourcode/codeLib/code/js_dateAdd.asp?catID=2
function dateAdd( start, interval, number ) {
	
    // Create 3 error messages, 1 for each argument. 
    var startMsg = "Sorry the start parameter of the dateAdd function\n"
        startMsg += "must be a valid date format.\n\n"
        startMsg += "Please try again." ;
		
    var intervalMsg = "Sorry the dateAdd function only accepts\n"
        intervalMsg += "d, h, m OR s intervals.\n\n"
        intervalMsg += "Please try again." ;

    var numberMsg = "Sorry the number parameter of the dateAdd function\n"
        numberMsg += "must be numeric.\n\n"
        numberMsg += "Please try again." ;
		
    // get the milliseconds for this Date object. 
    var buffer = Date.parse( start ) ;
	
    // check that the start parameter is a valid Date. 
    if ( isNaN (buffer) ) {
        alert( startMsg ) ;
        return null ;
    }
	
    // check that an interval parameter was not numeric. 
    if ( interval.charAt == 'undefined' ) {
        // the user specified an incorrect interval, handle the error. 
        alert( intervalMsg ) ;
        return null ;
    }

    // check that the number parameter is numeric. 
    if ( isNaN ( number ) )	{
        alert( numberMsg ) ;
        return null ;
    }

    // so far, so good...
    //
    // what kind of add to do? 
    switch (interval.charAt(0))
    {
        case 'd': case 'D': 
            number *= 24 ; // days to hours
            // fall through! 
        case 'h': case 'H':
            number *= 60 ; // hours to minutes
            // fall through! 
        case 'm': case 'M':
            number *= 60 ; // minutes to seconds
            // fall through! 
        case 's': case 'S':
            number *= 1000 ; // seconds to milliseconds
            break ;
        default:
        // If we get to here then the interval parameter
        // didn't meet the d,h,m,s criteria.  Handle
        // the error. 		
        alert(intervalMsg) ;
        return null ;
    }
    return new Date( buffer + number ) ;
}
//-------------------------------------------------